package test;

import java.util.ArrayList;
import java.util.List;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

public class HttpUtil {

	static RestTemplate re = new RestTemplate();

	public static String get(String url, String CASTGC) {

		try {
			HttpHeaders headers = new HttpHeaders();
			headers.add("User-Agent",
					"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36");
			List<String> cookies = new ArrayList<>();
			cookies.add("CASTGC=" + CASTGC);
			cookies.add("DeptCode=" + "hbhk3");
			cookies.add("UserCode=" + "hbhk2");

			headers.put(HttpHeaders.COOKIE, cookies);
			HttpEntity request = new HttpEntity(null, headers);
			ResponseEntity<String> result = re.exchange(url, HttpMethod.GET, request, String.class);

			return result.getBody();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "无数据";

	}

	public static String get(String url, String key, String val) {

		try {
			HttpHeaders headers = new HttpHeaders();
			List<String> cookies = new ArrayList<>();
			cookies.add(key + "=" + val);
			headers.put(HttpHeaders.COOKIE, cookies);
			HttpEntity request = new HttpEntity(null, headers);
			ResponseEntity<String> result = re.exchange(url, HttpMethod.GET, request, String.class);

			return result.getBody();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "无数据";

	}

	public static String post(String url, String CASTGC) {

		try {
			HttpHeaders headers = new HttpHeaders();
			List<String> cookies = new ArrayList<>();
			cookies.add("CASTGC=" + CASTGC);
			headers.put(HttpHeaders.COOKIE, cookies);
			HttpEntity request = new HttpEntity(null, headers);
			ResponseEntity<String> result = re.exchange(url, HttpMethod.POST, request, String.class);

			return result.getBody();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "无数据";

	}

	public static String json(String url, String json) {

		RestTemplate restTemplate = new RestTemplate();
//		restTemplate.setErrorHandler(new ResponseErrorHandler() {
//
//			@Override
//			public boolean hasError(ClientHttpResponse response) throws IOException {
//				System.out.println("2" + response.toString());
//				return false;
//			}
//
//			@Override
//			public void handleError(ClientHttpResponse response) throws IOException {
//				System.out.println("1" + response.toString());
//
//			}
//		});

		HttpHeaders headers = new HttpHeaders();
		headers.add("User-Agent",
				"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36");

		MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
		headers.setContentType(type);
		headers.add("Accept", MediaType.APPLICATION_JSON.toString());

		HttpEntity<String> formEntity = new HttpEntity<String>(json, headers);
		String result = restTemplate.postForObject(url, formEntity, String.class);
		return result;

	}

}
